Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
Displaying Images At Nodes To display images along with the text in TreeView nodes, you must associate an ImageList control with the TreeView object. This is one of the relatively simple aspects of working with a TreeView control. You need only to place an ImageList control on your form, then include code to load it with the needed images, assigning each one a unique Key property. (You learned about the ImageList control earlier in this chapter.) Then, associate the ImageList control with TreeView by setting the appropriate property. You can do this at design time, using the TreeView control’s Custom Properties dialog box, or at runtime, as follows:
    TreeView1.ImageList = ImageList1
Finally, each time you add a node to the tree, specify which image is to be displayed with the node, using the image’s Key property to refer to it. You can also specify a different image to be used when the node is open and displaying its child nodes, just as Windows Explorer displays open or closed file folder images as appropriate. This is done by setting the SelectedImage property to the desired image when the node is open. Where do you get the images? You can create your own, or you can use those supplied by Visual Basic, designed specifically for use with the TreeView control. They are installed in a subfolder off the main Visual Basic folder. Installation of these and other graphics is optional when Visual Basic is installed, so if you cannot find these images, you may need to repeat the Visual Basic installation. Here are the available images: • OPEN.BMP—An open file folder • CLOSED.BMP—A closed file folder • LEAF.BMP—A document page • PLUS.BMP—A plus sign • MINUS.BMP—A minus sign Working With Nodes At any given moment, a TreeView object has at least one selected node (assuming, of course, that you have added one or more nodes to it). The selected node can be specified in code, and it can be automatically set when the user clicks on a node. You might expect the highlight to move to the selected node, but this is not the case. Being selected and being highlighted are two separate functions as far as a node on a TreeView control is concerned. In many applications, however, you want them to coincide. You must do this in code, using the DropHighlight property as follows:
    Set TV.DropHighlight = Nothing
    Set TV.DropHighlight = node
The first line removes any existing highlight, and the second line highlights the node referenced by node. This code goes in an event procedure, but not in the TreeView control’s Click procedure. This procedure is called whenever the control is clicked, regardless of whether the click happened on a node or elsewhere on the control. Rather, you use the NodeClick event procedure, which is called only when a node is clicked:
    Private Sub TreeView1_NodeClick(ByVal Node As Node)
Keeping track of nodes—which one is selected, which one is highlighted, and so on—can indeed be a bit tricky. Often, you must use an independent program variable to hold the Index property of the currently selected node, as I have done in the demonstration program. Then, when the user clicks on another node to select it, you can determine the identity of the previously selected node in case you need to manipulate it in some way, such as removing its highlight. Associating Data With Nodes As I mentioned earlier, the TreeView control becomes a lot more useful when you have the capability of associating data with each node. At the simplest level, you can use each Node object’s Tag property to hold a chunk of text. While this is suitable for some applications, it is too limited to take you very far. TIP: Playing Tag The Tag property is potentially very useful, but is often ignored. Almost every Visual Basic control has this property, but its use is a mystery to most programmers. Actually, Visual Basic does not use the Tag property at all—it is provided purely for the convenience of the programmer. You can store anything you want in a control’s Tag property and use that information in any way you like. A better idea is to take advantage of the unique Index property that each Node object is automatically assigned when it is created. You can use this number as a link between each node and a specific item in any of Visual Basic’s data-storage methods that uses indexed retrieval. For example, each node’s Index could point to a record in a random access file or to an element in an array. Either of these methods would be pretty easy to work out. Potentially more interesting, I believe, is to combine a TreeView control with a control array. This is the method I have used in the demonstration program. Recall that a control array consists of two or more controls of the same type and name, distinguished from each other by their Index property. Thus, you could have 10 Text Box controls named MyText: MyText(0), MyText(1), and so on up to MyText(9). You can create a control array at design time, but you have more flexibility if you do it in code. You must place the first element of the array on a form during design and be sure to set its Index property to 0. Then, use the Load statement to create new control array elements. If the control you inserted at design time is named MyText, the code to add a new Text Box to the control array would be
    Load MyText(Index)
where Index is the index of the new control. The value of Index must be unique for each control in the array, of course, but the values do not have to be sequential. Thus, we can add a new node to a TreeView control, inserting it as a child of the currently selected node, and create a new Text Box to go with it as follows:
    Dim newNode as node
    Set newNode = TV.Nodes.Add(TV.SelectedItem.Index, _
            tvwChild, , newNodeName, pictureKey)
    Load MyText(newNode.Index)
There’s Lots More I wish I had the time and space to tell you more about the TreeView control, but the best I can do is point you in the right direction and let you explore on your own. The TreeView object has a variety of properties that control its appearance, letting you change, for example, the amount of indentation child nodes have with respect to their parent, how lines between nodes are drawn, and the like. You can also specify that all nodes at each level be sorted alphabetically. The Nodes collection has only a few properties and methods. You can add and remove nodes, clear all nodes at once, and determine the number of nodes present. Each individual Node object has a set of properties and methods that provide a great deal of flexibility to the programmer. You can, for example, determine the parent, children, root, and siblings of any node, as well as refer to the next and previous node in the Nodes collection. Using these properties, you can make modifications, such as deleting the current node and all its children, or using the drag-and-drop function to move nodes around in the tree.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.